from django.core.paginator import Paginator
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from goods.ser import *
from goods.models import *
from django.db.models import Q, F
# 分页
class DingList(APIView):
    def get(self, request):
        name = request.GET.get('name', None)
        page = int(request.GET.get('page', 1))
        size = int(request.GET.get('size', 2))
        # 定义从哪开始切
        data_start = (page - 1) * size
        # 定义切到哪
        data_end = page * size

        ding = DingdanModle.objects.all()[data_start:data_end]
        count = Goods.objects.count()

        ding_ser = DingSer(ding, many=True)
        return Response({'data': ding_ser.data, 'total': count})


class Login(APIView):
    def get(self, request):
        name = request.GET.get('name')

        user = UserModel.objects.filter(name=name).first()
        if user:
            return Response({'code': 200, 'message': '登陆成功', 'uid': user.id})
        return Response({'code': 300, 'message': '登陆失败'})
#
class Addgoods(APIView):
    def post(self, request):
        goods = GoodsSer(data=request.data)
        if goods.is_valid():
            goods.save()
            return Response({'code': 200, 'message': '成功'})
        return Response({'code': 401, 'message': '失败'})
#
#
class GoodsView(APIView):
    def get(self, request):
        goods = Goods.objects.all()
        ser = GoodsSer(goods, many=True)
        return Response({'code': 200, 'message': ser.data})

class GoodV(APIView):
    def get(self, request):
        name = request.GET.get('name')
        print(name)
        goodslist = Goods.objects.filter(Q(text__icontains=name) | Q(price__icontains=name))
        good_ser = GoodsSer(goodslist, many=True)
        return Response(good_ser.data)


#
#
#
class Ding(APIView):
    def get(self, request):
        uid = request.GET.get('uid')
        name = request.GET.get('name')
        shouji = request.GET.get('shouji')
        user = UserModel.objects.filter(id=uid).first()
        if user:
            if user.name == name and user.shouji == shouji:
                return Response({'code': 200, 'message': '身份验证通过', 'name': user.name, 'shouji': user.shouji})
            else:
                return Response({'code': 300, 'message': '身份错误'})
        return Response({'code': 300, 'message': '身份错误'})

class Dan(APIView):
    def get(self, request):
        name = request.GET.get('name')
        dingdan = DingdanModle.objects.filter(name=name).all()
        dinglist = DingSer(dingdan, many=True)
        return Response({'code': 200, 'message': dinglist.data})

    def post(self, request):
        d = DingSer(data=request.data)
        print(d)
        if d.is_valid():
            d.save()
            return Response({'code': 200, 'message': '成功'})
        return Response({'code': 401, 'message': '失败'})
#
#         # name = request.POST.get('name')
#         # shouji = request.POST.get('shouji')
#         # chanpin = request.POST.get('chanpin')
#         # print(name)
#         # ding = DingdanModle()
#         # ding.name = name
#         # ding.shouji = shouji
#         # ding.text = chanpin
#         # ding.save()
#         # return Response({'code': 200, 'message': '提交成功'})
#

